package com.cyy.demo.controller;

import com.cyy.demo.aspect.Log;
import com.cyy.demo.common.BusinessException;
import com.cyy.demo.common.Result;
import com.cyy.demo.entity.User;
import com.cyy.demo.filter.MyHttpSessionListener;
import com.cyy.demo.service.UserService;
import com.cyy.demo.validated.InsertGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(value="重点人员接口",tags = "接口信息",description = "重点人员相关接口")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Value("${hk.ip}")
    private String hkip;

    @Autowired
    private UserService userService;


    /**
     * 用户信息查询
     * @param id
     * @return
     */
    @Log(descrption = "查询用户信息",actionType = "4")
    @RequestMapping(value = "/getuser/{id}", method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ApiImplicitParam(name = "用户id",value = "id")
    @ApiOperation("查询用户")
    public Result getUser(@PathVariable int id) {
        return userService.findUser(id);
    }

    /**
     * 查询全部用户
     * @return
     */
    @RequestMapping(value = "/getusers", method = RequestMethod.GET)
    public String getUser() {
        return userService.findUsers().toJson();
    }

    /**
     * 测试异常
     * @return
     */
    @RequestMapping(value = "/teste", method = RequestMethod.GET)
    public String testException() {
        throw new BusinessException("故意的");
//        return "hello world";
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        return "test";
    }


    private String message ="hello world";

    @GetMapping("/asd/{name}")
    public String welcome(@PathVariable String name, Map<String, Object> model) {
        model.put("time", new Date());
        model.put("message", this.message);
        return "welcome";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Object foo() {
        LOGGER.info("打印日志----------------------");
        return  "login";
    }

    @RequestMapping("/index")
    @ResponseBody
    public Object index(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        session.setAttribute("zxc", "zxc");
        return  "index";
    }


    @RequestMapping(value = "/online" ,method = RequestMethod.GET ,produces = {"application/xml;charset=UTF-8"})
    @ResponseBody
    @ApiOperation(value = "获取菜单详情", response = String.class)
    public String online() {
        return  "当前在线人数：" + MyHttpSessionListener.online + "人";
    }


    @PostMapping("/add")
    public String addUser (@Validated(InsertGroup.class)  @RequestBody User user){
        userService.saveUser(user);
        return new Result().toJson();
    }


    @GetMapping("/get/{username}")
    public Result getUser(String username){
        return userService.findUser(username);
    }

    @PostMapping("/delete/{id}")
    public Result remove(@PathVariable("id") String id){
        userService.remove(Integer.parseInt(id));
        return new Result();
    }

    @PostMapping("update/{id}/{username}/{password}/{sex}")
    public Result update(@PathVariable("id") String id,
                         @PathVariable("username") String username,
                         @PathVariable("password") String password,
                         @PathVariable("sex") String sex ){
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUsername(username);
        user.setPassword(password);
        user.setSex(Integer.parseInt(sex));
        userService.update(user);
        return new Result();
    }


}
